package cc.comac.saic.hkhy.jsrj.gj.inttools.controller;

import java.io.IOException;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cc.comac.saic.hkhy.jsrj.gj.common.satoken.utils.LoginHelper;
import cc.comac.saic.hkhy.jsrj.gj.inttools.constant.FileType;
import cc.comac.saic.hkhy.jsrj.gj.inttools.core.R;
import cc.comac.saic.hkhy.jsrj.gj.inttools.pojo.vo.FileInfoShowVo;
import cc.comac.saic.hkhy.jsrj.gj.inttools.service.FileInfoService;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.Part;

/**
 * 文件上传
 *
 * @author shisongyan
 * @date 2025/7/10
 */

@RestController
@RequestMapping("file-upload")
public class FileUploadController {
    @Autowired
    private FileInfoService fileInfoService;

    /**
     * 8.1.4	高层需求模型上传接口
     */
    @PostMapping(value = "{projKey}/hlr-model", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<?> hlrModelUpload(@PathVariable String projKey, HttpServletRequest request) throws ServletException, IOException {
        Collection<Part> fileParts = request.getParts();

        List<FileInfoShowVo> upload = fileInfoService.upload(projKey, FileType.HLR_MODEL, fileParts, LoginHelper.getUserId());
        return R.ok(upload);
    }

    /**
     * 8.1.5	一致性检查报告上传接口
     */
    @PostMapping(value = "{projKey}/hlr-consistency", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<?> hlrConsistencyUpload(@PathVariable String projKey, HttpServletRequest request) throws ServletException, IOException {
        Collection<Part> fileParts = request.getParts();
        return R.ok(fileInfoService.upload(projKey, FileType.HLR_CONSISTENCY, fileParts, LoginHelper.getUserId()));
    }

    /**
     * 8.1.7	架构模型上传接口
     */
    @PostMapping(value = "{projKey}/archi-model", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<?> archiModelUpload(@PathVariable String projKey, HttpServletRequest request) throws ServletException, IOException {
        Collection<Part> fileParts = request.getParts();
        return R.ok(fileInfoService.upload(projKey, FileType.ARCHI_MODEL, fileParts, LoginHelper.getUserId()));
    }

    /**
     * 8.1.8	控制软件模型上传接口
     */
    @PostMapping(value = "{projKey}/control-model", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<?> controlModelUpload(@PathVariable String projKey, HttpServletRequest request) throws ServletException, IOException {
        Collection<Part> fileParts = request.getParts();
        return R.ok(fileInfoService.upload(projKey, FileType.CONTROL_MODEL, fileParts, LoginHelper.getUserId()));
    }

    /**
     * 8.1.10	显示软件模型上传接口
     */
    @PostMapping(value = "{projKey}/display-model", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<?> displayModelUpload(@PathVariable String projKey, HttpServletRequest request) throws ServletException, IOException {
        Collection<Part> fileParts = request.getParts();
        return R.ok(fileInfoService.upload(projKey, FileType.DISPLAY_MODEL, fileParts, LoginHelper.getUserId()));
    }

    /**
     * 8.1.9	控制模型生成代码上传接口
     */
    @PostMapping(value = "{projKey}/control-code", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<?> controlCodeUpload(@PathVariable String projKey, HttpServletRequest request) throws ServletException, IOException {
        Collection<Part> fileParts = request.getParts();
        return R.ok(fileInfoService.upload(projKey, FileType.CONTROL_CODE, fileParts, LoginHelper.getUserId()));
    }

    /**
     * 8.1.11	显示模型生成代码上传接口
     */
    @PostMapping(value = "{projKey}/display-code", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<?> displayCodeUpload(@PathVariable String projKey, HttpServletRequest request) throws ServletException, IOException {
        Collection<Part> fileParts = request.getParts();
        return R.ok(fileInfoService.upload(projKey, FileType.DISPLAY_CODE, fileParts, LoginHelper.getUserId()));
    }

    /**
     * 8.1.14	工程代码上传接口
     */
    @PostMapping(value = "{projKey}/impl-code", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<?> implCodeUpload(@PathVariable String projKey, HttpServletRequest request) throws ServletException, IOException {
        Collection<Part> fileParts = request.getParts();
        return R.ok(fileInfoService.upload(projKey, FileType.IMPL_CODE, fileParts, LoginHelper.getUserId()));
    }

    /**
     * 8.1.16	测试案例上传接口
     */
    @PostMapping(value = "{projKey}/test-case", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<?> testCaseUpload(@PathVariable String projKey, HttpServletRequest request) throws ServletException, IOException {
        Collection<Part> fileParts = request.getParts();
        return R.ok(fileInfoService.upload(projKey, FileType.TEST_CASE, fileParts, LoginHelper.getUserId()));
    }

    /**
     * 8.1.17	静态分析报告上传接口
     */
    @PostMapping(value = "{projKey}/static-report", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<?> staticReportUpload(@PathVariable String projKey, HttpServletRequest request) throws ServletException, IOException {
        Collection<Part> fileParts = request.getParts();
        return R.ok(fileInfoService.upload(projKey, FileType.STATIC_REPORT, fileParts, LoginHelper.getUserId()));
    }

    /**
     * 8.1.18	插桩代码上传接口
     */
    @PostMapping(value = "{projKey}/instr-code", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<?> instrCodeUpload(@PathVariable String projKey, HttpServletRequest request) throws ServletException, IOException {
        Collection<Part> fileParts = request.getParts();
        return R.ok(fileInfoService.upload(projKey, FileType.INSTR_CODE, fileParts, LoginHelper.getUserId()));
    }

    /**
     * 8.1.19	测试报告上传接口
     */
    @PostMapping(value = "{projKey}/test-report", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<?> testReportUpload(@PathVariable String projKey, HttpServletRequest request) throws ServletException, IOException {
        Collection<Part> fileParts = request.getParts();
        return R.ok(fileInfoService.upload(projKey, FileType.TEST_REPORT, fileParts, LoginHelper.getUserId()));
    }

}
